package com.lcq.gulimall.order.listener;

import com.lcq.common.to.OrderTo;
import com.lcq.gulimall.order.entity.OrderEntity;
import com.lcq.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@RabbitListener(queues = "order-release-queue")
@Slf4j
public class OrderReleaseListener {
    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void orderReleaseQueueConsumer(OrderTo orderTo, Channel channel, Message message) throws IOException {
        log.info(">>>>>>>>>>>>>>>close order :"+orderTo.getOrderSn());
        try{
            orderService.cancelOrder(orderTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }


    }
}
